package top.z.orm.handle.osql.core;

import top.z.orm.core.osql.BeeAbstractFactory;
import top.z.orm.handle.distribution.ds.Router;

import javax.sql.DataSource;

public class BeeFactory extends BeeAbstractFactory {

    private static BeeFactory instance;

    private static HoneyFactory honeyFactory;

    private BeeFactory() {
    }

    public void setHoneyFactory(HoneyFactory honeyFactory) {
        BeeFactory.honeyFactory = honeyFactory;
    }

    public static HoneyFactory getHoneyFactory() {
        if (honeyFactory == null) {
            honeyFactory = new HoneyFactory();
        }
        return honeyFactory;
    }

    public static BeeFactory getInstance() {
        if (instance == null) {
            instance = new BeeFactory();
        }
        return instance;
    }

    @Override
    public DataSource getDataSource() {
        if (super.getDataSourceMap() == null) {
            return super.getDataSource();
        } else {
            return _getDsFromDsMap();
        }
    }

    private DataSource _getDsFromDsMap() {
        String dsName = Router.getDsName();
        return getDataSourceMap().get(dsName);
    }
}